package com.lenovo.smarttraffic.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.contrarywind.view.WheelView;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.api.onDateChoose;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Caleb.F
 * on 2020-03-11
 * at 9:16
 */
public class PickerDateDialogFragment extends DialogFragment {
    private FrameLayout mdatePickerContainer;
    private Button mBtnCacel;
    private Button mBtnChoose;
    private Button mBtnConfirm;
    private TimePickerView mTimePickerView;
    private String dateInfo;
    private onDateChoose mOnDateChoose;
    private static final String PICKER_DIALOG = "com.lenovo.smarttraffic.ui.fragment.PickerDateDialogFragment";


    public static PickerDateDialogFragment newInstance(String argument) {
        Bundle bundle = new Bundle();
        bundle.putString(PICKER_DIALOG, argument);
        PickerDateDialogFragment pickerDateDialogFragment = new PickerDateDialogFragment();
        pickerDateDialogFragment.setArguments(bundle);
        return pickerDateDialogFragment;
    }

    @Override
    public void onStart() {
        super.onStart();
        getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dateInfo = getArguments().getString(PICKER_DIALOG);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.fragment_calendar_dialog, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mdatePickerContainer = (FrameLayout) view.findViewById(R.id.myFramelayout);
        initDataPicker();
    }


    private void initDataPicker() {
        Calendar selectCalendar = Calendar.getInstance();
        String[] split = dateInfo.split("-");
        selectCalendar.set(Integer.parseInt(split[0]),
                Integer.parseInt(split[1])-1,
                Integer.parseInt(split[2]));
        Calendar startCalendar = Calendar.getInstance(Locale.CHINA);
        startCalendar.set(2015, 4, 1);
        Calendar endCalendar = Calendar.getInstance(Locale.CHINA);
        endCalendar.set(2020, 11, 31);
        mTimePickerView = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
//实际上没有什么用
            }
        }).setRangDate(startCalendar, endCalendar)
                .setDate(selectCalendar)
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("年", "月", "日", "", "", "")
                .setDividerType(WheelView.DividerType.WRAP)
                .setTextColorCenter(Color.parseColor("#000000"))
                .setTextColorOut(Color.parseColor("#000000"))
                .setDividerColor(Color.parseColor("#35B6ED"))
                .setLineSpacingMultiplier(3f)
                .setLayoutRes(R.layout.dialog_data_picker, new CustomListener() {
                    @Override
                    public void customLayout(View convertView) {
                        mBtnCacel = convertView.findViewById(R.id.btn_cacel);
                        mBtnChoose = convertView.findViewById(R.id.btn_choose);
                        mBtnConfirm = convertView.findViewById(R.id.btn_confirm);
                    }
                })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        mBtnCacel.setOnClickListener(v -> {
                            dismiss();
                        });
                        mBtnConfirm.setOnClickListener(v -> {
                            mOnDateChoose.getDate(date);
                            dismiss();
                        });
                    }
                })
                .isCyclic(true)
                .isCenterLabel(true)
                .setDecorView(mdatePickerContainer)
                .build();
        mTimePickerView.show();

    }


    public void setonDateChoose(onDateChoose onDateChoose) {
        mOnDateChoose = onDateChoose;
    }
}
